using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.IO;

namespace ConvertSheetCS
{
    class Program
    {
        static void Main(string[] args)
        {
            // Make sure the correct number of command line arguments were specified.
            if (args.Length < 2)
            {
                ShowUsage();
                return;
            }

            string sourceDoc = args[0];
            string destinationDoc = args[1];
            DirectoryInfo dirInfo = new DirectoryInfo(@sourceDoc);
            FileInfo[] excelFiles = dirInfo.GetFiles("*.xls");

            //string targetDoc = args[1];
            //string targetFormat = args[2].ToUpper();
            //XlFixedFormatType xlSaveFormat = new XlFixedFormatType();

            // Make sure the target format is valid.
            /*switch (targetFormat)
            {
                case "PDF": xlSaveFormat = XlFixedFormatType.xlTypePDF;
                    break;

                case "XPS": xlSaveFormat = XlFixedFormatType.xlTypeXPS;
                    break;

                default: throw new Exception("The specified target format is not valid.");
            }*/

            Program p = new Program();
            foreach (FileInfo excelFile in excelFiles)
            {
                try
                {
                    System.Console.WriteLine(sourceDoc + "\\" + excelFile.ToString());
                    p.ConvertWorkbookToPDFXPS(sourceDoc + "\\" + excelFile.ToString(), destinationDoc + "\\" + excelFile.ToString());
                    Console.WriteLine("Conversion complete!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Conversion Error: " + ex.Message);
                }
            }
        }

        static void ShowUsage()
        {
            Console.WriteLine();
            Console.WriteLine("Usage: ExcelToPDF SourceFolderPath TargetFolderPath");
            Console.WriteLine();
            Console.WriteLine("SourceFolderPath    The fully qualified path to the Folder containing the excel Files.");
            Console.WriteLine("                 Enclose the path in quotes if it contains spaces.");
            Console.WriteLine();
            Console.WriteLine("TargetFolderPath   The fully qualified path to the folder which will contain the converted files.");
            Console.WriteLine("                 Enclose the path in quotes if it contains spaces.");
        }

        public void ConvertWorkbookToPDFXPS(string sourceBookPath, string destinationBookPath)
        {
            // Make sure the source document exists.
            if (!System.IO.File.Exists(sourceBookPath))
                throw new Exception("The specified source workbook does not exist.");

            // Create an instance of the Excel ApplicationClass object.          
            ApplicationClass excelApplication = new ApplicationClass();

            // Declare a variable to hold the reference to the workbook.
            Workbook excelWorkBook = null;

            // Declare variables for the Workbooks.Open and ApplicationClass.Quit method parameters. 
            //string paramSourceBookPath = @"D:\Dev\Work\Akona\How To's\HowTos\Excel\ConvertingSheetToPDFXPS\test.xlsx";
            string paramSourceBookPath = sourceBookPath;
            object paramMissing = Type.Missing;

            // Declare variables for the Workbook.ExportAsFixedFormat method parameters.
            //string paramExportFilePath = @"D:\Dev\Work\Akona\How To's\HowTos\Excel\ConvertingSheetToPDFXPS\test.pdf";
            //XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF;

            // To save the file in XPS format using the following for the paramExportFilePath
            // and paramExportFormat variables:
            //
            //string paramExportFilePath = @"D:\Dev\Work\Akona\How To's\HowTos\Excel\ConvertingSheetToPDFXPS\test.xps";
            string paramExportFilePath = destinationBookPath.Replace("xls", "pdf");
            XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF;
            
            XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityStandard;
            bool paramOpenAfterPublish = false;
            bool paramIncludeDocProps = true;
            bool paramIgnorePrintAreas = true;
            object paramFromPage = Type.Missing;
            object paramToPage = Type.Missing;

            try
            {
                // Open the source workbook.
                excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath, paramMissing, paramMissing, paramMissing,
                    paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing,
                    paramMissing, paramMissing, paramMissing, paramMissing, paramMissing);

                // Save it in the target format.
                if (excelWorkBook != null)
                    excelWorkBook.ExportAsFixedFormat(paramExportFormat, paramExportFilePath, paramExportQuality, paramIncludeDocProps,
                        paramIgnorePrintAreas, paramFromPage, paramToPage, paramOpenAfterPublish, paramMissing);
            }
            catch (Exception ex)
            {
                // Respond to the error.
                Console.WriteLine(ex.Message);
            }
            finally
            {
                // Close the workbook object.
                if (excelWorkBook != null)
                {
                    excelWorkBook.Close(false, paramMissing, paramMissing);
                    excelWorkBook = null;
                }

                // Close the ApplicationClass object.
                if (excelApplication != null)
                {
                    excelApplication.Quit();
                    excelApplication = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }
    }
}